/*
 * Created on 08-ago-2005
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package org.mushroomdb.query.impl;

import java.util.StringTokenizer;

import org.mushroomdb.catalog.Catalog;
import org.mushroomdb.catalog.Column;
import org.mushroomdb.catalog.Table;
import org.mushroomdb.engine.EvaluationEngine;
import org.mushroomdb.exception.RDBMSRuntimeException;
import org.mushroomdb.index.Index;
import org.mushroomdb.index.IndexService;
import org.mushroomdb.query.Query;
import org.mushroomdb.service.ServiceManager;


public class IndexDDLQuery extends Query {
	
	private String name;
	private Table table;
	private Column column;
	private int type;

	/**
	 * CREATE (UNIQUE | CLUSTERED) INDEX {index-name}
	 * USING (HASH | BTREE) ON {tabla}.{columna};
	 * 
	 * @param queryString
	 */
	public IndexDDLQuery(String queryString) {

		//queryString = queryString.replaceAll(" on "," ON ");
		StringTokenizer tokenizer = new StringTokenizer(queryString," ");
		
		String token = null;
		
		/* CREATE */
		if (tokenizer.hasMoreTokens()) {
			token = tokenizer.nextToken();
			if (!"CREATE".equals(token.toUpperCase())) {
				throw new RDBMSRuntimeException("Parsing error");
			}
		} else {
			throw new RDBMSRuntimeException("Parsing error");
		}
		
		/* UNIQUE | CLUSTERED */
		if (tokenizer.hasMoreTokens()) {
			token = tokenizer.nextToken();
			if (!"UNIQUE".equals(token.toUpperCase()) && !"CLUSTERED".equals(token.toUpperCase())) {
				throw new RDBMSRuntimeException("Parsing error");
			}
		} else {
			throw new RDBMSRuntimeException("Parsing error");
		}
		
		/* INDEX */
		if (tokenizer.hasMoreTokens()) {
			token = tokenizer.nextToken();
			if (!"INDEX".equals(token.toUpperCase())) {
				throw new RDBMSRuntimeException("Parsing error");
			}
		} else {
			throw new RDBMSRuntimeException("Parsing error");
		}
		
		/* {index-name} */
		if (tokenizer.hasMoreTokens()) {
			token = tokenizer.nextToken();
			if (token != null) {
				this.name = token;
			} else {
				throw new RDBMSRuntimeException("Parsing error");
			}
		} else {
			throw new RDBMSRuntimeException("Parsing error");
		}
		
		/* USING */
		if (tokenizer.hasMoreTokens()) {
			token = tokenizer.nextToken();
			if (!"USING".equals(token.toUpperCase())) {
				throw new RDBMSRuntimeException("Parsing error");
			}
		} else {
			throw new RDBMSRuntimeException("Parsing error");
		}
		
		/* HASH | TREE */
		if (tokenizer.hasMoreTokens()) {
			token = tokenizer.nextToken();
			if ("HASH".equals(token.toUpperCase())) {
				this.type = Index.HASH_TYPE;
			} else if ("TREE".equals(token.toUpperCase())) {
				this.type = Index.TREE_TYPE;
			} else {
				throw new RDBMSRuntimeException("Parsing error");
			}
		} else {
			throw new RDBMSRuntimeException("Parsing error");
		}
		
		/* ON */
		if (tokenizer.hasMoreTokens()) {
			token = tokenizer.nextToken();
			if (!"ON".equals(token.toUpperCase())) {
				throw new RDBMSRuntimeException("Parsing error");
			}
		} else {
			throw new RDBMSRuntimeException("Parsing error");
		}
		
		/* {table.column} */
		if (tokenizer.hasMoreTokens()) {
			token = tokenizer.nextToken();
			if (token.indexOf('.') > -1) {
				String tableName = token.substring(0,token.indexOf('.')).trim();
				String columnName = token.substring(token.indexOf('.') + 1).trim();
				Catalog catalog = Catalog.getInstance();
				this.table = catalog.getTableByName(tableName);
				this.column = table.getColumn(columnName);
			} else {
				throw new RDBMSRuntimeException("Parsing error");
			}
		} else {
			throw new RDBMSRuntimeException("Parsing error");
		}
		
//		String field = queryString.replaceAll(" on "," ON ");
//		field = field.replaceAll(";","");
//		field = field.substring(field.indexOf("ON ") + "ON ".length()).trim();
//		
//		String tableName = field.substring(0,field.indexOf('.')).trim();
//		String columnName = field.substring(field.indexOf('.') + 1).trim();
//		
//		Catalog catalog = Catalog.getInstance();
//		this.table = catalog.getTableByName(tableName);
//		this.column = table.getColumn(columnName);
//		
//		this.type = Index.TREE_TYPE;
	}

	public String getName() {
		return this.name;
	}
	
	public Column getColumn() {
		return this.column;
	}

	public Table getTable() {
		return this.table;
	}

	public int getType() {
		return this.type;
	}

	/**
	 * @see org.mushroomdb.query.Query#execute(org.mushroomdb.engine.EvaluationEngine)
	 */
	public Object execute(EvaluationEngine evaluationEngine) {
		
		String name = this.getName();
		Table table = this.getTable();
		Column column = this.getColumn();
		int type = this.getType();
		
		IndexService indexService =  (IndexService) ServiceManager.getInstance().getService(IndexService.class);
		indexService.createIndex(name,table,column,type);
		
		return Boolean.TRUE;
	}
}
